<?php



class AffiliatesettingAction extends CommonAction

{



    public function index()

    {

        $this->listmap .= " and site_id=" . $this->siteid;

        parent::_list();
		
		
        $this->display();

    }



    public function edit()

    {

		

        $id = $this->_get('id');

        $this->assign('AffiliateConfig', C('AffiliateConfig'));

        $db = M('Affiliatesetting');

        $info = $db->where(array('id' => $id))->find();

        import('@.ORG.Affiliate.' . $info['classname']);

        $affclass = $info['classname'];
		
        $aff = new $affclass(0, $this->siteid);

        $html = $aff->createform();

        $this->formhtml = $html;

        parent::edit();

    }



    public function update()

    {



        import('@.ORG.Affiliate.' . $this->_post('classname'));

        $affclass = $this->_post('classname');

        $aff = new $affclass(0, $this->siteid);



        if (!$aff->saveform()) {

            $this->error($aff->getError());

        }



        parent::update();

    }



    public function add()

    {

        $this->assign('AffiliateConfig', C('AffiliateConfig'));

        parent::add();

    }



    //分成明细

    public function afflist()

    {
        $Mal = D('Member_account_log');
        if ($this->_get('keywords')) {
            $this->listmap .= " and order_sn like '%".$this->_get('keywords')."%'";
        }
		if($this->_get('stime') || $this->_get('etime')){
	      $this->stime = $this->_get('stime');
		  $this->etime = $this->_get('etime');
		  $stime = strtotime($this->_get('stime'));
		  $etime = strtotime($this->_get('etime'))+24*3600;
		 if($this->_get('stime')){
		  $this->listmap.=" and pdstate=3 and dateline > $stime ";
		 }
		 if($this->_get('etime')){
		  $this->listmap.=" and pdstate=3 and dateline < $etime ";
		 }
		 if($this->_get('etime') && $this->_get('stime')){
		  $this->listmap.=" and pdstate=3 and dateline > $stime and dateline < $etime ";
		 }
	   }else{
	     $this->listmap.=" and pdstate=3";
	   }

	   $this->listmap.=" and status <> 3";
        $this->initView();
        $Mal->_listgroup($this->view, $this->listmap, 'order_id desc', 2000);
				
		//echo $Mal -> getLastSql();
        $this->display();
    }


    //订单会员分成明细

    public function orderafflist()

    {

        $Mal = D('Member_account_log');

        $map['order_id'] = $this->_get('order_id');

        $map['product_id'] = $this->_get('product_id');

        if ($this->_get('attr')) {

            $map['attr'] = $this->_get('attr');

        }

        $this->initView();

        $Mal->_list($this->view, $map, 'order_id desc', 20);





        $memberdb = M('Member');

        $list = $this->view->get('list');

        foreach ($list as $k => $v) {

            $member = $memberdb->where(array('id' => $v['member_id']))->Field('nickname,img_url')->find();

            $list[$k]['nickname'] = $member['nickname'];

            $list[$k]['headimg'] = $member['img_url'];

        }

        $this->assign('list', $list);

        $this->display();





    }



    //确认分成

    public function confirm()

    {

        //dump($this -> _post('order_id'));exit;

        if ($this->_post('order_id')) {

            $db = D('Member_account_log');

            $order_id = $_POST['order_id'];

            $attr = $_POST['attr'];

            $product_id = $_POST['product_id'];

            foreach ($order_id as $k => $v) {

                $db->confirm($v, $product_id[$k], $attr[$k]);

            }
			
            $this->success('操作完成！');
			
        } else {

            $this->error('没有可分成的订单！');

        }

    }
	 public function auto_confirm()

    {

        //dump($this -> _post('order_id'));exit;

        if ($this->_post('order_id')) {

            $db = D('Member_account_log');

            $order_id = $_POST['order_id'];

            $attr = $_POST['attr'];

            $product_id = $_POST['product_id'];

            foreach ($order_id as $k => $v) {

                $db->confirm($v, $product_id[$k], $attr[$k]);

            }
			
            
			
        } else {

            $this->error('没有可分成的订单！');

        }

    }


    public function setQrcode()

    {

        if ($this->_post('qrcode')) {

            $data['qrcodesetting'] = serialize($_POST);

            $map['id'] = $this->siteid;

            $db = M('Site');

            $db->data($data)->where($map)->save();

            //echo $db -> getLastSql();

            $info = $db->where($map)->find();

            $this->assign(unserialize($info['qrcodesetting']));

            $this->success('设置成功', U('setQrcode'));

        } else {

            $map['id'] = $this->siteid;

            $db = M('Site');

            $info = $db->where($map)->find();

            $this->assign(unserialize($info['qrcodesetting']));

            //dump($info);

            $this->display();

        }

    }

}